package com.fram.controller.api.android;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fram.bean.admin.android.goods.request.GoodsAndroidListRequest;
import com.fram.bean.admin.android.goods.request.GoodsAndroidRequest;
import com.fram.bean.admin.android.goods.request.GoodsSalesRequest;
import com.fram.bean.sys.PageBean;
import com.fram.bean.sys.ResponseMsg;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProException;
import com.fram.common.util.PublicMethod;
import com.fram.common.util.apiversion.ApiVersion;
import com.fram.common.util.base.BaseController;
import com.fram.entity.ProjectGoodsPrice;
import com.fram.service.api.v1.android.GoodsAndroidService;

@RestController
@RequestMapping("/api/{version}/app/manage/goods")
public class GoodsAndroidController extends BaseController{
	
	private static Logger LOG = LoggerFactory.getLogger(GoodsAndroidController.class);

	@Autowired
	private GoodsAndroidService goodsAndroidService;
	
	//
	@ApiVersion(1.0)
	@RequestMapping("sales")
	private ResponseMsg sales(@RequestBody GoodsSalesRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.setDistributorId(getDistributorId());
			msg.setData(goodsAndroidService.sales(request));
		} catch (ProException e) {
			LOG.error("-安卓商品销售状况表出错-->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("--安卓商品销售状况异常--->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	
	//
	@ApiVersion(1.0)
	@RequestMapping("list")
	private ResponseMsg list(@RequestBody PageBean<GoodsAndroidListRequest> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			System.out.println("sss"+getDistributorId());
			request.getParam().setDistributorId(getDistributorId());
			msg.setData(goodsAndroidService.list(request));
		} catch (ProException e) {
			LOG.error("-安卓商品列表出错-->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("--安卓商品列表异常--->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	
	@ApiVersion(1.0)
	@RequestMapping("add")
	private ResponseMsg add(@RequestBody GoodsAndroidRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.setDistributorId(getDistributorId());
			msg.setData(goodsAndroidService.add(request));
		} catch (ProException e) {
			LOG.error("-安卓增加商品出错-->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("--安卓增加商品异常--->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	
	@ApiVersion(1.0)
	@RequestMapping("detail")
	private ResponseMsg detail(Long id) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(goodsAndroidService.detail(id));
		} catch (ProException e) {
			LOG.error("-安卓商品详情出错-->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("--安卓商品详情异常--->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	
	@ApiVersion(1.0)
	@RequestMapping("update")
	private ResponseMsg update(@RequestBody GoodsAndroidRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			goodsAndroidService.update(request);
		} catch (ProException e) {
			LOG.error("-安卓商品修改出错-->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("--安卓商品修改异常--->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	
	@ApiVersion(1.0)
	@RequestMapping("price/add")
	private ResponseMsg priceAdd(@RequestBody List<ProjectGoodsPrice> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			goodsAndroidService.priceAdd(request);
		}catch (ProException e) {
			LOG.error("-安卓商品规格增加出错-->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("--商品规格增加异常--->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	@ApiVersion(1.0)
	@RequestMapping("price/detail")
	private ResponseMsg priceDetail(Long id) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(goodsAndroidService.priceDetail(id));
		} catch (ProException e) {
			LOG.error("-安卓商品规格详情出错-->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("--安卓商品规格详情异常--->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	@ApiVersion(1.0)
	@RequestMapping("price/update")
	private ResponseMsg priceUpdate(@RequestBody ProjectGoodsPrice request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			goodsAndroidService.priceUpdate(request);
		} catch (ProException e) {
			LOG.error("-安卓商品规格修改出错-->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("--安卓商品规格修改异常--->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
}
